﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Windows.Forms;

public partial class checkOut : System.Web.UI.Page
{
    string strconn = ConfigurationManager.ConnectionStrings["a123"].ToString();
    SqlConnection sqlconn;
    string text;
    protected void Page_Load(object sender, EventArgs e)
    {
        text = Request.QueryString["id"]; 
        if (!IsPostBack)
        {
            if (text == null)
            {
                bind();
            }
            else { binc(); }
            binf();
        }
    }
    public void bind()
    {
        Session["ID"] = "a001";
        sqlconn = new SqlConnection(strconn);
        sqlconn.Open();
        string sqlstr = "select * from tb_OrderInfo where userName='" + Session["ID"] + "' and IsConfirm='false'";
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "tb_OrderInfo");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "OrderID" };
        GridView1.DataBind();
        sqlconn.Close();
    }
    public void binc()
    {
        sqlconn = new SqlConnection(strconn);
        sqlconn.Open();
        string sqlstr = "select * from tb_OrderInfo where userName='" + Session["ID"] + "' and IsConfirm='false' and OrderID='" + text + "'";
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "tb_OrderInfo");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "OrderID" };
        GridView1.DataBind();
        sqlconn.Close();
    }
    public void binf()
    {
        sqlconn = new SqlConnection(strconn);
        sqlconn.Open();
        string sqlstr = "select * from tb_Member where UserName='" + Session["ID"] + "'";
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "tb_Member");
        DetailsView1.DataSource = myds;
        DetailsView1.DataKeyNames = new string[] { "UserName" };
        DetailsView1.DataBind();
        sqlconn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.TextBox textBox5 = (System.Web.UI.WebControls.TextBox)DetailsView1.Rows[0].FindControl("TextBox5");
        System.Web.UI.WebControls.TextBox textBox6 = (System.Web.UI.WebControls.TextBox)DetailsView1.Rows[0].FindControl("TextBox6");
        System.Web.UI.WebControls.TextBox textBox7 = (System.Web.UI.WebControls.TextBox)DetailsView1.Rows[0].FindControl("TextBox7");
        System.Web.UI.WebControls.TextBox textBox8 = (System.Web.UI.WebControls.TextBox)DetailsView1.Rows[0].FindControl("TextBox8");

        //Response.Write(textBox5.Text + textBox6.Text + textBox7.Text + textBox8.Text);
        sqlconn = new SqlConnection(strconn);
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.CheckBox checkbox = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            string sqlstr = "update tb_OrderInfo set IsConfirm='true',RealName='" + textBox5.Text + "',Phone='" + textBox6.Text + "',Address='" + textBox7.Text + "',beizhu='" + textBox8.Text + "' where UserName='" + Session["ID"] + "'and OrderID='" + GridView1.DataKeys[i].Value + "'";
            sqlconn.Open();
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlconn);
            sqlcom.ExecuteNonQuery();
            sqlconn.Close();
        }
        //bind();
        Response.Write("<script>alert('下单成功！')</script>");
    }
}